package atguigu.jdbc;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;

import java.beans.PropertyVetoException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

public class DruidDao extends DaoHandle{


    // 工具类，私有化无参构造函数
    private DruidDao() {
    }

    private static DruidDao databasePool = null;
    private static DruidDataSource dataSource = null;

    // 静态代码块，加载配置文件。
    static {
        try {
            //##########BOOT 更换连接池 ############
            dataSource = new DruidDataSource();

        } catch (Exception e) {
            throw new ExceptionInInitializerError(e);
        }
    }

    public static synchronized DruidDao getInstance() {
        if (null == databasePool) {
            databasePool = new DruidDao();
        }
        return databasePool;
    }

    /**
     * 创建数据库连接实例
     *
     * @return 数据库连接实例 connection
     */
    public DruidPooledConnection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            return null;
        }
    }

    @Override
    protected Connection getDao() throws IOException, PropertyVetoException, SQLException {
        return getConnection();
    }
}